package cn.itsource.org.controller;

import cn.itsource.org.domain.TbUser;
import cn.itsource.org.qury.UserQuery;
import cn.itsource.org.service.TbUserService;
import cn.itsource.org.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(description = "博客文档相关接口",tags = "用户管理")
public class TbUserController {
    @Autowired
    private TbUserService tbUserService;

    @PostMapping
    @ApiOperation(
            value = "用户新增或者修改",
            notes = "设置id就是修改，否则就是新增"
    )
    public AjaxResult save(@RequestBody TbUser user) {
        try {
            tbUserService.save(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败,请稍后再试!");
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult delet(@PathVariable("id")Long id){
        try {
            tbUserService.deleteByPrimaryKey(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败,请稍后再试!");
        }
    }
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody UserQuery userQuery){
        try {
            return AjaxResult.me().setResultObj(tbUserService.pageInfo(userQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败,请稍后再试!");
        }
    }

    @PatchMapping ("/batchDelete")
    public AjaxResult BatchDelete(@RequestBody List<TbUser> users){
        try {
            tbUserService.BatchDelete(users);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败,请稍后再试!");
        }
    }
    @PostMapping("/exist")
    public AjaxResult exist(@RequestBody TbUser user){
        try {
            return AjaxResult.me().setResultObj(tbUserService.exist(user));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败,请稍后再试!");
        }
    }
}
